<?php

namespace App\Http\Controllers\Business;

use App\Http\Controllers\Controller;
use App\Models\Business\PlatformType;
use App\Common\Traits\Model\ModelSearchTrait;
use Illuminate\Http\Request;

class PlatfomTypeController extends Controller
{
    use ModelSearchTrait;

    //
    public function index(Request $request, PlatformType $platformType)
    {
        $platformType=$this->singleModelSearch($request,$platformType,$this->platformCondition());
        $platforms=$platformType->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($platforms);
    }
    //
    public function platformCondition()
    {
        return [
            ['platform_name','like']
        ];
    }

    //
    public function store(Request $request)
    {
        if(!$request->filled('platform_name')){
            return $this->failed('类型名称不能为空',201);
        }
        $data=['platform_name'=>mb_substr($request->platform_name,0,64)];
        $platform=PlatformType::create($data);
        return $this->success(['msg'=>'添加成功','id'=>$platform->id]);
    }

    //
    public function show(Request $request)
    {
        $type=PlatformType::find($request->platform_id??0);
        return $this->success($type);
    }

    //
    public function update(Request $request, PlatformType $platformType)
    {
        if(!$request->filled('platform_name')){
            return $this->failed('类型名称不能为空',201);
        }
        $platform=$platformType->find($request->platform_id??0);
        if(empty($platform)){
            return $this->failed('id错误，无此类型',201);
        }
        $platform->platform_name=mb_substr($request->platform_name,0,64);
        $platform->save();
        return $this->success(['msg'=>'编辑成功']);
    }

    //
    public function destroy(Request $request, PlatformType $platformType)
    {
        $platform=$platformType->find($request->platform_id??0);
        if(empty($platform)){
            return $this->failed('id错误，无此类型',201);
        }
        $platform->delete();
        return $this->success(['msg'=>'删除成功']);
    }

    //
    public function getAllPlatforms(){
        $plateforms=PlatformType::all();
        return $this->success($plateforms);
    }


}
